package org.example.repository;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.task.SyncTaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

@EnableAspectJAutoProxy
public class TestConfig {

    private static final boolean enableAsync = false;

    @Bean
    public TestExecutor testExecutor() {
        return new TestExecutor(newExecutor());
    }

    private Executor newExecutor() {
        if (enableAsync) {
            ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
            executor.setCorePoolSize(4);
            executor.setMaxPoolSize(4);
            executor.setKeepAliveSeconds(0);
            executor.setQueueCapacity(Integer.MAX_VALUE);
            executor.setThreadNamePrefix("test-thread-");
            return executor;
        } else {
            return new SyncTaskExecutor();
        }
    }

    @Bean
    public TestAspect testAspect() {
        return new TestAspect();
    }
}
